package com.example.oauth2demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 使用OAUTH的安全配置
 *
 * @author karidyang
 * @since 1.0
 * Date: 2021-04-27
 **/
@Configuration
public class OAuth2SecurityConfig implements CustomWebSecurityConfigurer {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/about").hasRole("USER").and()
                .oauth2Login()
                .successHandler((httpServletRequest, httpServletResponse, authentication) -> {
                    System.out.println(authentication.getCredentials());
                    System.out.println(authentication.getAuthorities());
                });

        System.out.println("OAuth2SecurityConfig [" + http + "]");
    }

}
